'use strict';
const Controller = require('egg').Controller;
const exportExcel = require('../../utils/exportExcel');

function toInt(str) {
  if (typeof str === 'number') return str;
  if (!str) return str;
  return parseInt(str, 10) || 0;
}

class EducationController extends Controller {

  async create() {
    const ctx = this.ctx;
    const params = ctx.request.body;
    const res = await ctx.model.Education.bulkCreate(params);
    ctx.body = {
      code: 0,
      res,
      message: '人员教育信息登记成功',
    };
  }

  async index() {
    const { ctx } = this;
    const {
      personnelId,
      userName,
      pageSize = 10,
      current = 1,
    } = ctx.query;
    const limit = Number(pageSize);
    const offset = limit * ((current || 1) - 1);
    const query = {};
    if (personnelId) { query.where = { ...query.where, personnelId }; }
    if (userName) { query.where = { ...query.where, userName }; }
    const data = await ctx.model.Education.findAll({ ...query, limit, offset });
    const totalData = await ctx.model.Education.findAndCountAll();
    ctx.body = {
      code: 0,
      data,
      pageInfo: {
        total: totalData.count,
        current,
        pageSize: Number(pageSize),
      },
      message: 'success',
    };
  }
  async show() {
    const { ctx } = this;
    const detail = await ctx.model.Education.findByPk(ctx.params.id, { raw: true });
    ctx.body = {
      code: 0,
      message: 'success',
      data: detail,
    };
  }
  async update() {
    const ctx = this.ctx;
    const id = toInt(ctx.params.id);
    const delivery = await ctx.model.Education.findByPk(id);
    if (!delivery) {
      ctx.body = {
        code: 1,
        data: [],
        message: '没有找到结果',
      };
      return;
    }
    const params = ctx.request.body;
    const data = await delivery.update(params);
    ctx.body = {
      code: 0,
      data,
      message: '更新数据成功',
    };
  }

  async download() {
    const { ctx } = this;
    // excel表名
    const title = '教育信息';

    const headers = [
      [
        { t: '人员编号', f: 'id', totalRow: true },
        { t: '姓名', f: 'userName', totalRow: true },
        { t: '性别', f: 'sex', totalRow: true },
        { t: '联系手机', f: 'phone', totalRow: true },
        { t: '区域地址', f: 'regionalAddress', totalRow: true },
        { t: '专兼职', f: 'isFullTime', totalRow: true },
        { t: '身份证号', f: 'idNo', totalRow: true },
        { t: '备注', f: 'mark', totalRow: true },
      ],
    ];
    // 注：f的字段名需要跟数据库的字段一致

    const data = await ctx.model.Personnel.findAll();
    const newData = data.map(item => {
      if (item.sex === '1') {
        item.sex = '男';
      } else {
        item.sex = '女';
      }
      if (item.isFullTime === '1') {
        item.isFullTime = '专职';
      } else if (item.isFullTime === '2') {
        item.isFullTime = '兼职';
      }
      return item;
    });

    const res = await ctx.service.exportExcel.excelCommon(headers, newData, title);
    ctx.body = res;
  }

  async destroy() {
    const ctx = this.ctx;
    const id = toInt(ctx.params.id);
    const delivery = await ctx.model.Education.findByPk(id);
    if (!delivery) {
      ctx.body = {
        code: 1,
        data: [],
        message: '删除失败',
      };
      return;
    }

    await delivery.destroy();
    ctx.body = {
      code: 0,
      message: '删除成功',
    };
  }
}

module.exports = EducationController;
